package mysql

import (
	"gorm.io/gorm"
	"video-guard/videorpc/videorpc"
)

type CommentInfo struct {
	gorm.Model
	VideoId        int    `gorm:"type:int(11);not null;comment: 视频ID"`
	UserId         int    `gorm:"type:int(11);not null;comment:用户ID"`
	CommentContent string `gorm:"type:varchar(500);not null;;comment: 评论内容"`
	ReplyId        int64  `gorm:"type:int(11);not null;comment: 是否回复ID匿名"`
	CommentStatus  int64  `gorm:"type:tinyint;default:0;comment:  评论状态"`
	IsAnonymous    int64  `gorm:"type:tinyint;default:0;comment: 是否匿名"`
}

func (i *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Create(i).Error
}

func (i *CommentInfo) CommentDel(db *gorm.DB, in *videorpc.CommentDelTRequest) error {
	return db.Where("comment_infos.user_id=?", in.UserId).Where("comment_infos.video_id=?", in.VideoId).Delete(i).Error
}

func CommentList(db *gorm.DB, in *videorpc.CommentListTRequest) []*videorpc.CommentList {
	var list []*videorpc.CommentList
	db.Model(&CommentInfo{}).Select("user_infos.nick_name ,user_infos.avatar_link,video_infos.video_intro,video_infos.title,comment_infos.comment_content").
		Joins("LEFT JOIN user_infos ON comment_infos.user_id = user_infos.id").
		Joins("LEFT JOIN video_infos ON video_infos.id = comment_infos.video_id").Scopes(Paginate(int(in.Page), int(in.Size))).
		First(&list)
	return list
}
